package com.kingyea.mobilepolice.system.exception;


import com.kingyea.app.market.utils.StateCode;
import com.kingyea.mobilepolice.system.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * @author Mr.Lin
 * @version v1.0.0
 * @description
 * @date Created in 2018/2/23 16:11
 */
@ControllerAdvice
public class GlobalExceptionHandler {

    private static final Logger LOGGER = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    @ExceptionHandler(value = Throwable.class)
    @ResponseBody
    public Result jsonErrorHandler(HttpServletRequest req, Throwable e) throws Exception {
        Result r = new Result();
        if (e instanceof BusinessException) {
            BusinessException be = (BusinessException) e;
            r.setCode(be.getCode());
            r.setMsg(be.getMsg());
            r.setData(be.getObj());
        } else {
            r.setCode(StateCode.SYSTEM_ERROR);
            r.setMsg(StateCode.getMsg(StateCode.SYSTEM_ERROR));
            r.setData(e.toString());
        }
        e.printStackTrace();
        return r;
    }

}